
$().ready(function() {
	$('#timezone').change(function() {
		var gmtHours = $(this+":selected").val();
		$.ajax({
			url: "/includes/set-cookie.php?name=GMTClient&value="+gmtHours,
			success: function(data)
			{
				location.reload();	
			}
		});
	});
});

var PlayingChannelId;
function play(id,url)
{
	if(id == "" || url=="")
		return;
	if(id == PlayingChannelId)
		return;
		
	$("#chanel_"+PlayingChannelId+".sel").removeClass("sel");
	//$("#chanel_"+PlayingChannelId+" .rateContainer").fadeOut();
	$("#chanel_"+id).addClass("sel");
	//$("#chanel_"+id+" .rateContainer").fadeIn();
	PlayingChannelId = id;
	
	
	var code = '<object '
				+' id="MediaPlayer" width="600" height="450" '
				+' classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" '
				+' standby="Loading Microsoft Windows Media Player components..." '
				+' type="application/x-oleobject"> '
				+' <param name="URL" value="'+url+'"> '
				+' <param name="AutoStart" value="true"> '
				+' <param name="Balance" value="0"> '
				+' <param name="DisplaySize" value="0"> '
				+' <param name="Mute" value="false"> '
				+' <param name="PlayCount" value="0"> '
				+' <param name="Rate" value="1.0"> '
				+' <param name="ShowAudioControls" value="true"> '
				+' <param name="ShowControls" value="true"> '
				+' <param name="ShowDisplay" value="true"> '
				+' <param name="ShowStatusBar" value="true"> '
				+' <param name="ShowTracker" value="true"> '
				+' <param name="StretchToFit" value="false"> '
				+' <param name="TransparentAtStart" value="true"> '
				+' <param name="Volume" value="50"> '
				+' <param name="wmode" value="opaque"> '
				+' <param name="windowlessVideo" value="true"> '
				+' <embed type="application/x-mplayer2" '
				+' 	name="mediaplayer" '
				+' 	pluginspage="http://www.microsoft.com/Windows/MediaPlayer" '
				+'	src="'+url+'" '
				+'	Width="600" '
				+'	Height="450" '
				+'	AutoStart="1" '
				+'	Balance="0" '
				+'	DisplaySize="0" '
				+'	Mute="0" '
				+'	PlayCount="0" '
				+'	Rate="1.0" '
				+'	ShowAudioControls="1" '
				+'	ShowControls="1" '
				+'	ShowDisplay="1" '
				+'	ShowStatusBar="1" '
				+'	ShowTracker="1" '
				+'	StretchToFit="0" '
				+'	TransparentAtStart="1" '
				+'	windowlessvideo="1"  wmode="opaque"'
				+'	Volume="50" > '
				+'</embed> '
			+'</object>';

	$("#player").html(code);	
	$("#MediaPlayer").focus();
}

	
// slowEach() plugin by "Michael Geary"
// http://groups.google.com/group/jquery-en/browse_thread/thread/1790787be89b92bc/876c4ba8f01d8443#msg_72c18cfa10b7d7b6
// callback2 - fired after last element, can be used to build functions chain (added by Orkan)
jQuery.fn.slowEach = function(interval, callback, callback2) {
	var items = this, i = 0;
	if(!items.length) return;
	function next() {
		(callback.call(items[i], i, items[i]) !== false && ++i < items.length) ? setTimeout(next, interval) : callback2 && callback2.call(items, i, items);
	}
	next();
};
			
function initRating(target_id){
	var numStar = 5 ;
	// Create stars for: Rate this
	$("#stars-wrapper_"+target_id).stars({
		split: 2,
		cancelShow: false,
		callback: function(ui, type, value)
		{
			// Disable Stars while AJAX connection is active
			ui.disable();
			
			var arr = ui.$stars.find("a");

			arr.slowEach(10, 
				function(){ $(this).animate({top: "20px"}, 300) }, 
				function()
				{
					$("#chanel_"+target_id+" .rateLoader").fadeIn(function()
					{
						$.ajax({
						   type: "POST",
						   url: "/includes/rating.php",
						   data: ({value : value, target_id : target_id}),
							beforeSend: function(){
								
							}, 
							success: function(obj)
							{ 
								var data = jQuery.parseJSON(obj);
								
								ui.select(Math.round(data.avg));
							
								$("#chanel_"+target_id+" .rateLoader").fadeOut(function(){ 
									arr.slowEach(10, function(){ $(this).animate({top: 0}, 300) });
								});
								
								$("#chanel_"+target_id+" .stars-on").width(Math.round( $("#chanel_"+target_id+" .stars-off").width() / numStar * parseFloat(data.avg) ));
									
								//$("#rateContainer").html($(".meta-rating").html());
							}
						});	
						
						
					});
				});
				
				/*
			
			
			*/
			
		}
	});
	
	initAvgRating(target_id) ;

}

function initAvgRating(target_id)
{
	var numStar = 5 ;
	$.ajax({
	   type: "GET",
	   url: "/includes/rating.php",
		data: ({ action:"getRating" , target_id : target_id}),
		success: function(obj)
		{ 
			var data = jQuery.parseJSON(obj);
			
			$("#chanel_"+target_id+" .stars-on").width(Math.round( $("#chanel_"+target_id+" .stars-off").width() / numStar * parseFloat(data.avg) ));
			
		}
	});	
	
}


function initChatBox(){
	$('.chatboxcontainer').html('<div class="chatbox"><div class="turn_chat_box">Ẩn khung chat</div><div id="chatiframe"><iframe  src="/chat/index.php"/></div></div>');	
	
	$('.turn_chat_box').click(function() {
		$('#chatiframe').slideToggle('slow', function() {
		
			$('.turn_chat_box').text($('.turn_chat_box').text() == 'Ẩn khung chat' ? 'Hiện khung chat' : 'Ẩn khung chat');
			
		});
		
	});
}